package tests;

import common.Untils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Set;

public class MyProjectPage extends Untils {
    public static String url = "http://8.155.1.153:8081/project";
    public int questionnairenum;


    public MyProjectPage() {
        super(url);
    }

    public void createQuestionnaire() throws InterruptedException {
        Thread.sleep(2000);
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[1]/div[1]/span/div/div/button")).click();
        Thread.sleep(2000);
        //用这两种都没有办法找到该元素,为了后续测试可以直接跳转到后续url,但是后文已经有解决办法
//        driver.findElement(By.xpath("/html/body/div[6]/div/div/ul/li[1]/span")).click();
//        driver.findElement(By.cssSelector("body > div:nth-child(10) > div > div > ul > li:nth-child(1) > span")).click();
        //最笨的办法,直接跳转到想要进入的页面
//        driver.get("http://8.155.1.153:8081/survey/new?mode=survey");
        //网上学的新方法
        driver.findElement(By.xpath("//span[contains(text(), '问卷调查')]")).click();
        Thread.sleep(2000);
        //添加单选题目
        driver.findElement(By.xpath("//*[@id=\"rc-tabs-0-panel-1\"]/div/div/dl[1]/div/div[1]/dd/div/span")).click();
        //修改标题
        SimpleDateFormat sim = new SimpleDateFormat("mm-ss-SS");
        String title = sim.format(System.currentTimeMillis());
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[1]/div[2]/div[1]/div/pre/div[1]/p")).clear();
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[1]/div[2]/div[1]/div/pre/div[1]/p")).sendKeys(title);
        //修改单选题题目
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div[1]/div/div[1]/div[1]/div[2]/div/pre/div[1]/p")).clear();
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div[1]/div/div[1]/div[1]/div[2]/div/pre/div[1]/p")).sendKeys("这是一个单选题");
        Thread.sleep(2000);
        //修改单选题选项
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[1]/div/div/div[1]/div[1]/div/pre/div[1]/p")).clear();
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[1]/div/div/div[1]/div[1]/div/pre/div[1]/p")).sendKeys("这是选项一");
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[2]/div/div/div[1]/div[1]/div/pre/div[1]/p")).clear();
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[2]/div/div/div[1]/div[1]/div/pre/div[1]/p")).sendKeys("这是选项二");
        //对问卷进行预览
        driver.findElement(By.xpath("//*[@id=\"editor\"]/div[2]/div[1]/div[2]/div/button[3]/span")).click();
        //返回编辑界面,并进行保存即可发布
        driver.findElement(By.xpath("//*[@id=\"editor\"]/div[2]/div[1]/div[2]/div/button[1]/span")).click();
        driver.findElement(By.xpath("//*[@id=\"editor\"]/div[2]/div[1]/div[2]/div/button[4]/span")).click();
        //跳转到项目页
        driver.get("http://8.155.1.153:8081/project");
        //重新加载页面
        driver.get(url);
        //更换展示模式
        Thread.sleep(5000);
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[1]/div[2]/div/div/div/div/div/div/div/div/label[2]/span[2]/span")).click();
        Thread.sleep(3000);
        List<WebElement> num =  driver.findElements(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[2]/div/div/div/div[1]/div/div/div/div/div/div/table/tbody/tr"));
        questionnairenum = num.size();
        String afterTitle = driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[2]/div/div/div/div[1]/div/div/div/div/div/div/table/tbody/tr[" +num.size() + "]/td[1]")).getText();
    }
    public void fillQuestionNaire() throws InterruptedException, IOException {
        //找到这个问卷并点击进入
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[2]/div/div/div/div[1]/div/div/div/div/div/div/table/tbody/tr[" + questionnairenum + "]")).click();
        //开启问卷填写
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/button")).click();
        //打开问卷链接
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[1]/span[1]/span/span/button/span")).click();
        //此时已经打开新的页面,需要给driver切换句柄
        String currentHandle = driver.getWindowHandle();
        Set<String> allHandles =  driver.getWindowHandles();
        for (String x:allHandles) {
            if (x != currentHandle){
                driver.switchTo().window(x);

            }
        }
        //选择第一个选项
        Thread.sleep(3000);
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div/div/div[4]/div/section/div[2]/div/div/div/div[1]/label/span[2]/div")).click();
        //点击提交
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div/div/div[5]/div[2]/div/button/div")).click();
        Thread.sleep(3000);
        screenshot(Thread.currentThread().getStackTrace()[1].getMethodName());
        driver.close();
        //把句柄切换回主界面
        driver.switchTo().window(currentHandle);
    }
}
